package com.interauto.service.alarm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.interauto.bean.AutoAlarmBean;
import com.interauto.bean.autoenum.HttpEnum;
import com.interauto.bean.httpinfo.HttpRequestBean;
import com.interauto.util.HttpUtil;
import com.interauto.util.RSAUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.dc.pr.PRError;

import java.util.Map;
import java.util.TreeMap;

import static com.interauto.bean.autoenum.HttpEnum.HttpType.HTTP;
import static com.interauto.bean.autoenum.HttpEnum.HttpType.HTTPS;

/**
 * 处理报警发送相关的
 */
@Component
public class AlarmSendService {
    @Value("${autoalarm.alarmurl}")
    private String sendurl;

    private static final Logger logger= LoggerFactory.getLogger(AlarmSendService.class);


    public void sendalarmmessage(String message){
        logger.info(String.format("-->sendalarmmessage info is:%s",message));
        if(StringUtils.isEmpty(message)){
            return;
        }
        try{
            AutoAlarmBean autoalarminfo= JSON.parseObject(message,AutoAlarmBean.class);
            sendalarmforhttp(autoalarminfo);
        }
        catch (Exception e){
            e.printStackTrace();
            logger.error("sendalarmmessage is system error;message is:"+message);
        }
    }


    private void sendalarmforhttp(AutoAlarmBean autoalarminfo) throws Exception{
        Map<String,String> map = new TreeMap<>();
        String alarmtopic=autoalarminfo.getAlarmtopic();
        map.put("source",StringUtils.isEmpty(alarmtopic)?"MK-weld-qa":alarmtopic);
        map.put("receiver_type","0");
        String callnumber=autoalarminfo.getCallnumber();
        map.put("receiver",callnumber);
        map.put("organizationIds","");
        map.put("title","监控异常");
        map.put("content",autoalarminfo.getAlarmmessage());
        JSONObject obj = new JSONObject();
        obj.put("textType",false);
        obj.put("type","10002");
        //obj.put("content","包子测试哈哈");
        obj.put("createTime",String.valueOf(System.currentTimeMillis()));
        obj.put("remarkUrl",autoalarminfo.getAlarmurl());
        obj.put("remarkOpenPc","0");
        map.put("messageData",JSONObject.toJSONString(obj));
        String requestParams = JSONObject.toJSONString(map);
        System.out.println("入参字符串为:"+requestParams);
        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBANJpVdh+TgaGjcpM5kHsmY4iTxMFx4QDpd4nmdYL0JP5O792KAYbPpUG5+N88DuzSPo8PQfAHqyxah0sW2pEz5JISOurJTzMDbUjsc04SjoBnVYfBwYfoP9fz5DOCm7xN2yF9ORBqiCNZVVmACbtzEt1dH6Io902vL0k9hlEUDutAgMBAAECgYAbqzJYt7M29HasxZP5000V/bj8PWRRHdGv9vEpzn/tFXcf5D+7HdYK7s+LgKBumVLdZRJ5dHAUMXhMwcp7nPTWgHcCHTPP/UePreTgh9isucJS8ml9AjMN5mPJKTZjiqMkrBiwhRTd1YAcMvxZTZ1pjwGbMBuiiGtK8A5PEinEQQJBAPbdQKtk6A0Dk7hQcHN768ojoOaLpBBPePmbKN5vEMT3I93QYnODjaq0kiE2OAksRrWClExJKu/adyFKm1GscWUCQQDaMrxH/ybohX9sUHonzrRUnWSE7i5w/M14gzKddJwbiVHjVZ8KxITpwxH0gkaGUb1x2E2jWQOMhKleO+KxreCpAkBqd+gRQ5bVLgrCvmaRWAvMhWs6o1Uh97ScKnAI5uvblx8v5lTfrwglbHVtv32RbzgbxnKBKDlNcvJYOF4ebnoFAkBS+nZlvt+yD4qsy1IZL1+eyhg/AUFCELGnAeXOwVjYk/lDRf1sn92X9GownRZq4Ez2LXchBqn110F9p7KHNYO5AkEAg2hX5iNub91Z3ZnsbSCBhQ4OoSx4LIH6s+yGzYw8hyCjQNm4qdY/G6iSlDIoWuOLMhTxnTZkNm4Pb9xE0zIA/Q==";
        String verifySignature = RSAUtils.sign(requestParams.getBytes("UTF-8"), privateKey);
        map.put("verifySignature",verifySignature);
        String result = HttpUtil.doPost(sendurl, map);
        logger.info("sendalarmforhttp response is-->"+result);

//        HttpRequestBean httpRequestBean=new HttpRequestBean();
//        httpRequestBean.setHttpurl(sendurl);
//        httpRequestBean.setHttptype(HTTPS);
//        httpRequestBean.setPostparamtype(2);
//        httpRequestBean.setHttpmeth(HttpEnum.HttpMeth.POST);


    }
}
